package utils;

import java.util.Random;

import structure.concrete.Constant;
import structure.concrete.Cosine;
import structure.concrete.Divide;
import structure.interfaces.INode;
import structure.concrete.Minus;
import structure.concrete.Multiply;
import structure.concrete.Plus;
import structure.concrete.Sine;
import structure.concrete.Variable;

@SuppressWarnings("rawtypes")
public class Utils {

	public static Random rand = new Random();
	
	// arrays of the possible operators and possible nullary operators
	public static final Class[] operators = { Constant.class, Cosine.class, Divide.class, Minus.class, Multiply.class, Plus.class, Sine.class, Variable.class};
	public static final Class[] nullaryOperators = { Constant.class, Variable.class};

	/**
	 * @return random operator node
	 */
	@SuppressWarnings("unchecked")
	public static INode getRandomOperator() {
		INode node = null;
		try {
			int index = rand.nextInt(operators.length);
			Class<INode> c = operators[index];
			node = c.newInstance();
		} catch (Exception e) { 
			e.printStackTrace(); 
		}
		return node;
	}

	/**
	 * @return random nullary operator (constant or variable)
	 */
	public static INode getRandomNullaryOperator() {
		INode node = null;
		try {
			int index = rand.nextInt(nullaryOperators.length);
			@SuppressWarnings("unchecked")
			Class<INode> c = nullaryOperators[index];
			node = c.newInstance();
		} catch (Exception e) {	
			e.printStackTrace(); 
		}
		return node;
	}
}